﻿#include "OutputBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

OutputBlock::OutputBlock(QString &name, QRect &position, double zoom,QString &state, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    setState(state);
}

OutputBlock::~OutputBlock()
{

}

void OutputBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = 50 * zoom();
    double b = static_cast<double>(m_bScale)  * a;
    double bottom = y + height;
    double lineScale=static_cast<double>(m_lineScale);
    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + lineScale * zoom()));
    drawBottomArrow(canvas, QPointF(hcenter, y + lineScale * zoom()),
                                QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));

    QPointF par[4];
    par[0] = QPointF(hcenter - b/2 + a/4, y + lineScale * zoom());
    par[1] = QPointF(hcenter + b/2 + a/4, y + lineScale * zoom());
    par[2] = QPointF(hcenter + b/2 - a/4, y + lineScale * zoom() + a);
    par[3] = QPointF(hcenter - b/2 - a/4, y + lineScale * zoom() + a);
    if(runState().compare("RunSuccessfully") == 0)
    {
        QPen pe(m_runSuccessColor);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }
    else if(runState().compare("RunError") == 0)
    {
        QPen pe(m_runErrorColor);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }

    if(0 <= getReferenceStatus())
    {
        QColor color = m_unReferencedColor[0];
        if(m_unReferencedColor.size() >= getReferenceStatus())
        {
            color = m_unReferencedColor[getReferenceStatus()];
        }
        QPen pe(color);
        pe.setWidthF(2);
        canvas->setPen(pe);
    }

    QBrush brush = canvas->brush();
    canvas->setBrush(m_backgroundColor);
    canvas->drawPolygon(par, 4);
    canvas->setBrush(brush);

    UseOldPen
    QRectF textRect(hcenter - b/2 + a/4 +4, y + lineScale * zoom()+4, b-a/2 - 8, a - 8);
    QStringList ls = name().split(",");
    QString text = ls.join(", ");
    canvas->drawText(textRect, Qt::AlignCenter | Qt::TextWrapAnywhere, text);

    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y + lineScale * zoom()+a, hcenter, bottom+0.5));
    UseOldPen
}

QString OutputBlock::getBlockDom()
{
    return  "<algorithm><branch><ou id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" state=\"normal\" isPlugin=\"true\"/></branch></algorithm>";
}
}
}
